import axios from 'axios';
import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
	history: createWebHashHistory(),
	routes: [
		{
			path: '/',
			redirect: '/layout'
		},
		{
			path:'/layout',
      name: 'layout',
			component: () => import('@/layouts/MainLayout.vue'),
      redirect: '/index',
      children: [
        { path: '/index', name: 'index', component: () => import('@/views/index/index.vue') },
        { path: '/detail', name: 'detail', component: () => import('@/views/detail/index.vue') },
      ]
		}
	]
});

router.beforeEach((to, from, next) => {
  //记录页面访问
  const pageUrl = window.location.href;
  console.log('访问的页面：', pageUrl)

  //发送给后端保存
  axios.post('http://localhost:8771/api/history/record', {
    url: pageUrl,
    visitTime: new Date()
  }).then(response => {
    console.log('访问记录保存成功:', response.data)
  }).catch(error => {
    console.log('访问记录保存失败:', error)
  })

  next()
})
export default router;
